/*
 * Created by JFormDesigner on Sun Oct 19 17:07:56 CST 2025
 */

package com.yeban.javase.day10.swing_learn.event;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author yeban
 */
public class EventDemo003 extends JFrame {

    public static void main(String[] args) {
        (new EventDemo003()).setVisible(true);
    }

    public double sum(double a, double b) {
        return a + b;
    }

    public EventDemo003() {
        initComponents();
    }

    // 鼠标点击事件
    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        double i = Double.parseDouble(num1.getText());
        double j = Double.parseDouble(num2.getText());
        double sum = sum(i, j);
        if(sum == (int)sum) {
            result.setText(String.valueOf((int) sum));
        } else {
            result.setText(String.valueOf(sum));
        }
    }

    // 键盘按下事件
    private void button1KeyPressed(KeyEvent e) {
        // TODO add your code here
        double i = Double.parseDouble(num1.getText());
        double j = Double.parseDouble(num2.getText());
        double sum = sum(i, j);
        if(sum == (int)sum) {
            result.setText(String.valueOf((int) sum));
        } else {
            result.setText(String.valueOf(sum));
        }
    }

    private void buttonActionPerformed(ActionEvent e) {
        // TODO add your code here
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        num1 = new JTextField();
        label1 = new JLabel();
        button1 = new JButton();
        num2 = new JTextField();
        result = new JTextField();

        //======== this ========
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- num1 ----
        num1.setBackground(Color.white);
        contentPane.add(num1);
        num1.setBounds(40, 70, 65, 25);

        //---- label1 ----
        label1.setText("+");
        label1.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(label1);
        label1.setBounds(105, 70, 25, 25);

        //---- button1 ----
        button1.setText("=");
        button1.addActionListener(e -> {
			buttonActionPerformed(e);
			button1ActionPerformed(e);
		});
        button1.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                button1KeyPressed(e);
            }
        });
        contentPane.add(button1);
        button1.setBounds(210, 70, 45, 25);

        //---- num2 ----
        num2.setBackground(Color.white);
        contentPane.add(num2);
        num2.setBounds(135, 70, 65, 25);

        //---- result ----
        result.setBackground(Color.white);
        contentPane.add(result);
        result.setBounds(265, 70, 70, 25);

        contentPane.setPreferredSize(new Dimension(515, 410));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
        this.getRootPane().setDefaultButton(button1);
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JTextField num1;
    private JLabel label1;
    private JButton button1;
    private JTextField num2;
    private JTextField result;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
